#region Summary

// 
// Owner : JackieHan 
// 
// Solution : ResearchTest 
// 
// Project : JackieHan.ConfOrm.Test
// 
// File : TestManyToMany.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 测试多对多关联
// 
// History : 
//			2011-06-28 17:58:25	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using ConfOrm;
using ConfOrm.NH;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;

#endregion

namespace JackieHan.ConfOrm.Test.ManyToMany
{
    /// <summary> 
    /// 测试多对多关联
    /// </summary>
    [TestFixture]
    public class TestManyToMany
    {

        [SetUp]
        public void SetUp()
        {
        }

        [Test]
        public void TestJustConfSingle()
        {
            var mapping = GetMappingSingle();
            Console.WriteLine(NhConfig.Serializer(mapping));
        }

        private HbmMapping GetMappingSingle()
        {
            var orm = new ObjectRelationalMapper();
            var mapper = new Mapper(orm);
            var entityTypes = new[] { typeof(Person), typeof(Address) };

            orm.TablePerClass(entityTypes);
            orm.ManyToMany<Person, Address>();
            return mapper.CompileMappingFor(entityTypes);
        }

        [Test]
        public void TestJustConfDuplex()
        {
            var hbmMapper = GetMappingDuplex();
            Console.WriteLine(NhConfig.Serializer(hbmMapper));
        }

        public HbmMapping GetMappingDuplex()
        {
            var orm = new ObjectRelationalMapper();
            var mapper = new Mapper(orm);
            var entityTypes = new[] { typeof(User), typeof(Role) };

            orm.TablePerClass(entityTypes);
            orm.ManyToMany<User, Role>();
            var hbmMapper = mapper.CompileMappingFor(entityTypes);
            return hbmMapper;
        }

        [Test]
        public void TestDuplex()
        {
            var config = NhConfig.ConfigureNhiberate();
            config.AddDeserializedMapping(GetMappingDuplex(), "duplex");

            SchemaMetadataUpdater.QuoteTableAndColumns(config);

            new SchemaExport(config).Create(true, true);

            new SchemaExport(config).Drop(false, true);
        }

        [TearDown]
        public void TearDown()
        {
        }
    }
}